//
//  ZZXLoactionDetailPageViewController.m
//  Travel
//
//  Created by dllo on 15/11/30.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "ZZXLoactionDetailPageViewController.h"
#import "ZZXstrategryModel.h"
#import "ZZXLocationPageCollectionViewCell.h"
#import "ZZXLoactionThirdViewController.h"
#import "CXNetHandle.h"
#import "UIImageView+WebCache.h"
#import "ZZXLocationHeaderCollectionReusableView.h"

@interface ZZXLoactionDetailPageViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong)ZZXstrategryModel *guidemodel;
@property (nonatomic, strong)NSMutableArray *modelNiceGuideArr;
@property (nonatomic, strong)UIImageView *backImg; //北京图片
@property (nonatomic, strong)UIVisualEffectView *bV; //高斯模糊
@property (nonatomic, strong)UIButton *btn;

@end

@implementation ZZXLoactionDetailPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //左按钮
   self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"leftBack"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backToLast)];
    //小菊花
    [CXLoadMBProgress loadMBProgress:self.view];
      [self addScreenEdgePan];
    [self getData];
    
}
//数据请求
- (void)getData
{
    NSString *url1 = [NSString stringWithFormat:@"http://app6.117go.com/demo27/php/discoverAction.php?start=&length=12&submit=localityHome6_2&locid=%@&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=144629fa99d7f149d9082d1f6a747eff", self.itemid];
    
    [CXNetHandle getUrl:url1 cookie:nil result:^(id result) {
        
        self.guidemodel = [[ZZXstrategryModel alloc]init];
        [ self.guidemodel setValuesForKeysWithDictionary: result[@"obj"][@"head"][@"locality"]];
        
        [self getDataTwo];
    }];
    
    }

- (void)getDataTwo
{

    NSString *url2 = [NSString stringWithFormat:@"http://app6.117go.com/demo27/php/discoverAction.php?start=&subtype=-1&length=12&submit=getLocalityTours&locid=%@&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=1352e105ccabb70fee0447c786660da0", self.itemid];
    
    NSString *url3 = [NSString stringWithFormat:@"http://app6.117go.com/demo27/php/stickerAction.php?start=&length=12&submit=getSceneryHome&sceneryid=%@&v=i6.4.2&vc=AppStore&vd=b3c1b4cf8b498fb8&lang=zh-Hans&verify=024bb6753df57af7ba6df739621ae0a0", self.itemid];

    [CXNetHandle getUrl:url2 cookie:nil result:^(id result) {
        if (result[@"obj"][@"tags"] != nil) {
            
            self.modelNiceGuideArr = [NSMutableArray array];
            NSMutableArray *tempArr = [NSMutableArray array];
            for (NSMutableDictionary *dic in   result[@"obj"][@"list"]) {
                [tempArr addObject:dic];
            }
            self.modelNiceGuideArr = [ZZXstrategryModel transformWithArray: tempArr];
            
            [self createView];
    
    }
        
    }];
    
    [CXNetHandle getUrl:url3 cookie:nil result:^(id result) {
        if (result[@"obj"][@"tags"] == nil)
        {
            self.modelNiceGuideArr = [NSMutableArray array];
            NSMutableArray *tempArr = [NSMutableArray array];
            for (NSMutableDictionary *dic in   result[@"obj"][@"list"]) {
                [tempArr addObject:dic];
            }
            self.modelNiceGuideArr = [ZZXstrategryModel transformWithArray: tempArr];
            [CXLoadMBProgress removeMBProgress:self.view];
            [self createView];
        }
    }];
    
}
#pragma mark - 创建collectionView
- (void)createView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((CWIDTH - 30) / 2, (CWIDTH - 30)/ 2 + 80);
    layout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
    layout.headerReferenceSize = CGSizeMake(self.view.frame.size.width, 70);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) collectionViewLayout:layout];
    self.collectionView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.95 blue:0.9 alpha:1];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[ZZXLocationPageCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    [self.collectionView registerClass:[ZZXLocationHeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    //设置tableView的contentView距离上边界264
    self.backImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, -200, CWIDTH, 200)];
    
    NSString *url = [NSString stringWithFormat:@"%@%@%@",self.guidemodel.picdomain,@"upload/",self.guidemodel.picfile];
    
    [self.backImg sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"city"]];
    self.backImg.contentMode = UIViewContentModeScaleAspectFill;//设置img高度改变时 宽度也改变
    //裁剪
    self.backImg.clipsToBounds = YES;
    self.backImg.userInteractionEnabled = YES;
    [self.collectionView addSubview:self.backImg];

    //高斯模糊
    UIBlurEffect *b = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.bV = [[UIVisualEffectView alloc] initWithEffect:b];
    self.bV.alpha = 0.7;
    self.bV.frame = self.view.frame;
    [self.backImg addSubview:self.bV];
    
    //创建返回按钮
    self.btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //    self.btn.frame = CGRectMake(13, 25, 30, 30);
    //    [self.backImg addSubview:self.btn];
    self.btn.frame = CGRectMake(13, 25, 30, 30);
    [self.view addSubview:self.btn];
    [self.btn setImage:[UIImage imageNamed:@"leftBack"] forState:UIControlStateNormal];
    [self.btn addTarget:self action:@selector(backToLast) forControlEvents:UIControlEventTouchUpInside];
    
    /*国家城市地区名字*/
    UILabel *labelLocation = [[UILabel alloc]initWithFrame:CGRectMake(10, -130, CWIDTH - 20, 30)];
    labelLocation.textAlignment = NSTextAlignmentCenter;
    labelLocation.text = self.guidemodel.dispname;
    labelLocation.textColor = [UIColor whiteColor];
    labelLocation.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
    [self.collectionView addSubview:labelLocation];

    //接收地点数据
    NSMutableArray *arr = [NSMutableArray array];
    for (NSMutableDictionary *dic in self.guidemodel.path) {
        [arr addObject: dic[@"dispname"]];
    }
    
    for (NSInteger i = 0; i < arr.count; i++) {
        UILabel *LableBigLocation = [[UILabel alloc]initWithFrame:CGRectMake(CWIDTH / 2 + i * 65, - 35, 60, 25)];
        LableBigLocation.center = CGPointMake(CWIDTH / 2 + i * 65, - 13);
        LableBigLocation.text = arr[i];
        LableBigLocation.font = [UIFont systemFontOfSize:10];
        LableBigLocation.textAlignment = NSTextAlignmentCenter;
        LableBigLocation.layer.masksToBounds = YES;
        LableBigLocation.layer.cornerRadius = 10;
        LableBigLocation.backgroundColor = [UIColor grayColor];
        LableBigLocation.alpha = 0.8;
        LableBigLocation.textColor = [UIColor whiteColor];
        [self.collectionView addSubview:LableBigLocation];
   
    }
    
//    [[self  class]BoolIsNight:self.collectionView];
    
}

#pragma mark - 背景图下滑改变宽高
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取当前偏移量
    CGFloat y = scrollView.contentOffset.y;
    if (y < - 200 ) {
        CGRect frame = self.backImg.frame;
        frame.origin.y = y; //image 的frame不断往上偏移
        frame.size.height = -y;//collectionview向下偏移了多少 高度就增加多少
        self.backImg.frame = frame;
        
        self.bV.alpha = (286 + y) / 86 - 0.3;
      
        
    }

      //bar 将要出来时  显隐度一点点改变
    if (scrollView.contentOffset.y <= 0 && scrollView.contentOffset.y >= -64) {
        self.navigationController.navigationBar.alpha = (64 + scrollView.contentOffset.y) / 64;
    }
    else if (scrollView.contentOffset.y > 0)
    {
        self.navigationController.navigationBar.alpha = 1.0;
    }
    else
    {
        self.navigationController.navigationBar.alpha = 0.0;
    }
    
}

#pragma mark - 按钮触发方法
- (void)backToLast
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

#pragma mark - 视图要消失时候 设置bar出现
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.alpha = 1.0;
     self.navigationController.navigationBarHidden = NO;
}

#pragma mark - 视图要出现时候 隐藏
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.alpha = 0.0;
    
#pragma mark - 视图将要出现 夜间模式
    
//    if ([[NSUserDefaults standardUserDefaults]boolForKey:@"isNight"])
//    {
//        self.collectionView.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
//        self.view.backgroundColor = [UIColor colorWithRed:0.28 green:0.28 blue:0.28 alpha:1];
//        
//    }
//    else
//    {
//        self.collectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
//        self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.97 blue:0.93 alpha:1];
//    }
//    [self.collectionView reloadData];
}

#pragma mark - collectionView的头
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader) {
        
       ZZXLocationHeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    
        return headerView;
    }
    else
    {
        return nil;
    }
}

#pragma mark - item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.modelNiceGuideArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
   ZZXLocationPageCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor whiteColor];
    cell.guideModel = self.modelNiceGuideArr[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZZXLoactionThirdViewController *thirdDetail = [[ZZXLoactionThirdViewController alloc]init];
    
    thirdDetail.listID = [self.modelNiceGuideArr[indexPath.item] listId];
    
    thirdDetail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:thirdDetail animated:YES];
    
}

#pragma mark-滑动手势1
- (void)addScreenEdgePan
{
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(presentLeftMenuViewController:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    [self.collectionView addGestureRecognizer:screenEdgePan];
    
}

#pragma mark-滑动手势2
-(void)presentLeftMenuViewController:(UIScreenEdgePanGestureRecognizer *)edge
{
    [self.navigationController popViewControllerAnimated:YES];
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
